package cn.edu.scujcc.customofsichuan;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.edu.scujcc.customofsichuan.Adapter.CollectionAdapter;
import cn.edu.scujcc.customofsichuan.Entity.Collection;
import cn.edu.scujcc.customofsichuan.Listener.ItemClickListener;

public class SecondPager_Mine_Collection extends AppCompatActivity{

    public static List<Collection> CollectionEntity=new ArrayList<>();
    RecyclerView collectionRecyclerView;
    CollectionAdapter collectionAdapter;
    SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_secondpager_mine_collection);

        sharedPreferences=getSharedPreferences("thirdPage_Detail",Context.MODE_PRIVATE);
        //顶部导航栏返回键
       Toolbar toolbar=findViewById(R.id.collection);
       toolbar.setNavigationOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               finish();
           }
       });
        //初始化RecyclerView
        initRecyclerView();

        CollectionEntity.add(new Collection(R.drawable.collection_2,"放水节传承保护","ThridPager_huobajie_protect","2022/06/23"));
        CollectionEntity.add(new Collection(R.drawable.collection_2,"大佛节传承保护","dafojie_protect","2022/05/18"));
        CollectionEntity.add(new Collection(R.drawable.collection_2,"燃灯节历史渊源","dafojie_history","2022/05/08"));


    }

    private void initAnimation() {
        Animation animation= AnimationUtils.loadAnimation(SecondPager_Mine_Collection.this,R.anim.collection_anim);
        LayoutAnimationController layoutAnimationController=new LayoutAnimationController(animation);
        layoutAnimationController.setOrder(LayoutAnimationController.ORDER_NORMAL);
        layoutAnimationController.setDelay(0.2f);
        collectionRecyclerView.setLayoutAnimation(layoutAnimationController);
    }

    private void initRecyclerView() {
        collectionRecyclerView = findViewById(R.id.collection_list);
        //必须设置布局管理者，否则无法看到数据
        collectionRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        collectionAdapter=new CollectionAdapter(CollectionEntity);
        collectionRecyclerView.setAdapter(collectionAdapter);

        //初始化数据
        createCollection();
        //设置加载动画
        initAnimation();

        collectionAdapter.setItemClickListener(new ItemClickListener() {

            @Override
            public void onItemClick(View view, int position) {
                //获取上下文
                Context context = view.getContext();
                Intent intent =new Intent(context,thirdPage_Detail.class);
                Bundle bundle=new Bundle();
                bundle.putString("title",sharedPreferences.getString("title",null));
                bundle.putString("webUrl",sharedPreferences.getString("webUrl",null));
                intent.putExtras(bundle);
                startActivity(intent);


                View collection_more = view.findViewById(R.id.collection_more);
                //为每个子项目的更多按钮添加监听事件
                collection_more.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        AlertDialog.Builder builder=new AlertDialog.Builder(context);
                        View v = LayoutInflater.from(context).inflate(R.layout.collecrion_dialog, null);
                        builder.setView(v);
                        AlertDialog dialog = builder.create();
                        dialog.show();

                        Window window = dialog.getWindow();

                        window.setBackgroundDrawableResource(R.drawable.dialog_shape);

                        //只能通过调用show方法返回的对象调用cancel方法才能关闭对话框
                        //并且只能关闭当前对话框，无法同时关闭当前和父级对话框
                        //AlertDialog cancel_dialog = dialog.show();
                        View collection_cancel = v.findViewById(R.id.collection_cancel);
                        View collection_share = v.findViewById(R.id.collection_share);

                        //取消收藏事件
                        collection_cancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                //删除shared preference中的数据
                                SharedPreferences.Editor edit = sharedPreferences.edit();
                                edit.putString("title",null);
                                edit.putString("webUrl",null);
                                edit.putBoolean("collection",false);
                                edit.commit();

                                //删除集合中的数据
                                CollectionEntity.remove(position);
                                //关闭对话框
                                dialog.cancel();
                                collectionAdapter.notifyItemRemoved(position);
                                collectionAdapter.notifyItemChanged(position);
                                //为了数据同步防止错位
                                collectionAdapter.notifyDataSetChanged();

                            }
                        });
                        //分享事件
                        collection_share.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                //关闭上一级对话框
                                dialog.cancel();
                                View dialog_share = LayoutInflater.from(context).inflate(R.layout.dialog_share, null);
                                builder.setView(dialog_share);
                                builder.setTitle("分享给朋友");
                                AlertDialog dialog = builder.create();
                                dialog.show();
                                //获取的是对话框的背景样式
                                Window window=dialog.getWindow();
                                window.setBackgroundDrawableResource(R.drawable.share_background);
                                //对话框设置动画
                                window.setWindowAnimations(androidx.appcompat.R.style.Animation_AppCompat_DropDownUp);
                               //获取屏幕宽度
                                DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
                                WindowManager.LayoutParams wlp = window.getAttributes();
                                wlp.gravity= Gravity.BOTTOM;
                                wlp.width=displayMetrics.widthPixels;
                                window.setAttributes(wlp);

                            }
                        });

                    }
                });

            }
        });

    }

    private void createCollection() {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy/MM/dd");
        String currentTime = simpleDateFormat.format(new Date());

        boolean isCollected = sharedPreferences.getBoolean("collection", false);
        String title = sharedPreferences.getString("title", null);
        String collection_currentClass = sharedPreferences.getString("collection_currentClass", null);
        if (isCollected==true){
            Collection collection = new Collection(R.drawable.collection_2, title, collection_currentClass, currentTime);
            CollectionEntity.add(collection);
        }

    }

    //防止同一个收藏重复添加，故该界面销毁时将集合清空
    @Override
    protected void onDestroy() {
        super.onDestroy();
        CollectionEntity.clear();
    }
}